package com.kaifamiao.month05.day07;

import lombok.Data;

public class ThreadTest {


    public static void main(String[] args) {
        MyRunnable run = new MyRunnable(true); // 加
        MyRunnable sRun = new MyRunnable(); // 减
        Thread a1 = new Thread(run,"加法线程一");
        Thread a2 = new Thread(run,"加法线程二");
        Thread s1 = new Thread(sRun,"减法线程一");
        Thread s2 = new Thread(sRun,"减法线程二");
        a1.start();
        a2.start();
        s1.start();
        s2.start();
        System.out.println(MyRunnable.getJ());
    }
}

@Data
class MyRunnable implements Runnable {

    private static int j = 0;
    private boolean flag; // true 加  false 减

    public MyRunnable() {
        this(false);
    }
    public MyRunnable(boolean flag) {
        this.flag = flag;
    }

    public static int getJ() {
        return j;
    }

    @Override
    public void run() {
        if (flag) {
            for (int i = 0; i < 100; i++) {
                j++;
            }
            System.out.println(Thread.currentThread().getName() + ":" + j);
        } else  {
            for (int i = 0; i < 100; i++) {
                j--;
            }
            System.out.println(Thread.currentThread().getName() + ":" + j);
        }
    }
}


